#include "ultils.h"

void simu_key(unsigned char kb_id,unsigned short ch)
{
    struct input_event m_event;
    int fd_kb=0;// /dev/input/eventx (cat /proc/bus/input/devices)
    char kb_dev[255]={'\0'};
    sprintf(kb_dev,"/dev/input/event%d",kb_id);
    enable_dev(kb_dev);
    fd_kb = open(kb_dev,O_RDWR,0);
    if(fd_kb<=0)
    {
        printf("open kb failed!\n");
        return;
    }
    m_event.type=EV_KEY;
    m_event.code=ch;
    m_event.value=1;//key_up=0,keydown=1 repeat keydown=2
    gettimeofday(&m_event.time, NULL);
    write(fd_kb,&m_event,sizeof(m_event));

    m_event.type=EV_SYN;
    m_event.code=SYN_REPORT;
    m_event.value=0;
    write(fd_kb,&m_event,sizeof(m_event));

    memset(&m_event,0,sizeof(m_event));
    gettimeofday(&m_event.time, NULL);
    m_event.type=EV_KEY;
    m_event.code=ch;
    m_event.value=0;//key_up=0,keydown=1 repeat keydown=2
    write(fd_kb,&m_event,sizeof(m_event));

    m_event.type=EV_SYN;
    m_event.code=SYN_REPORT;
    m_event.value=0;
    write(fd_kb,&m_event,sizeof(m_event));

    close(fd_kb);
}
//enable keyboard devices
//use cat /proc/bus/input/devices to find out input devices
void enable_dev(const char* strDev)
{
    char chCmd[255]={'\0'};
    sprintf(chCmd,"sudo chmod 666 %s",strDev);
    system(chCmd);
}

void getFormatTimeNow( char *pTimenow)
{
    struct timeval tv;
    char strTime[32]={0};

    gettimeofday(&tv, NULL);

    struct tm *ptm = gmtime(&tv.tv_sec);  //将秒转换成struct tm的形式
    strftime(strTime, 32, "%F %T", ptm);
    // printf("%s ", strTime); //输出精确到秒
    //printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒
    sprintf(pTimenow, "%s.%ld", strTime, tv.tv_usec/1000);
}

void getTimeNow(unsigned int* year,unsigned char* mon,unsigned char* day,
                unsigned char* hour,unsigned char* min,unsigned char* sec)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm *lm = localtime(&tv.tv_sec);  //将秒转换成struct tm的形式

    if(year) *year = (int)lm->tm_year + 1900;
    if(mon) *mon = (unsigned char)lm->tm_mon + 1;
    if(day) *day = (unsigned char)lm->tm_mday;
    if(hour) *hour = (unsigned char)lm->tm_hour;
    if(min) *min = (unsigned char)lm->tm_min;
    if(sec) *sec = (unsigned char)lm->tm_sec;
}
unsigned long long GetTickCount()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec*1000+tv.tv_usec/1000;
}
void BytesToHexStr(unsigned char *bBuffer,int bLen,char *strBuf)
{
     unsigned char l4b,r4b;
     char l4c,r4c;
     int i;
     for(i=0;i<bLen;i++)
     {
          r4b=bBuffer[i] & 0x0f;
          if(r4b<=9)
              r4c=0x30+r4b;
          else
              r4c=0x41+(r4b-0x0a);

          l4b=(bBuffer[i] & 0xf0) >> 4;
          if(l4b<=9)
              l4c=0x30+l4b;
          else
              l4c=0x41+(l4b-0x0a);

          strBuf[i*2]=l4c;
          strBuf[i*2+1]=r4c;

     }
}
//
//  函数CharToInt说明
//  转字符成数值
//
int CharToInt(char val)
{
     int ret=-1;
     if(val>='0' && val <='9')
     {
         ret=val-0x30;
     }
     if(val>='A'  && val<='F')
     {
         ret=val-'A'+ 0x0a;
     }
     if(val>='a' && val<='f')
     {
         ret=val-'a'+0x0a;
     }

     return ret;

}
bool HexStrToBytes(const char *strBuf,unsigned char byBuf[],int &byLen)
{
     int                      slen=0;
     int                      byteCount;
     int                      index=0;
     unsigned char            btmp;
     int                      val;
     int                      itmp =0;

     slen=(int)strlen(strBuf);
     if(slen<2)              return false;
     //不是2的倍数
     if((slen % 2)!=0)       return false;

     byteCount=slen / 2;

     if(byLen > 0 && byteCount > byLen)
         byteCount = byLen;

     itmp=byteCount;
     //全部转大写
     //CharToUpper(strBuf);
     while(byteCount)
     {
          btmp=0;
          val=CharToInt(strBuf[index]);
          if(val==-1)  return false;
          btmp=(val << 4) & 0xf0;

          val=CharToInt(strBuf[index+1]);
          if(val==-1)  return false;

          btmp = btmp | (val & 0x0f);

          byBuf[itmp-byteCount] =btmp;

          index=index+2;

          byteCount--;
     }

     byLen=itmp;

     return true;

}

void Numeric2Bytes(const unsigned long &num,unsigned char *bytes,int byts_Len,bool small_head)
{
     if(small_head)//小端
     {
         for (int i=0;i<byts_Len;i++) {
             bytes[i]=(num >> (i*8)) & 0xff;
         }
     }
     else
     {
         for (int i=0;i<byts_Len;i++) {
             bytes[i]=(num >> ((byts_Len-i-1)*8)) & 0xff;
         }
     }
}
bool Bytes2Numeric(const unsigned char *bytes,int blen,bool small_head,unsigned long &num)
{
     if(small_head)//小端
     {
         for (int i=0;i<blen;i++) {
             num |= (bytes[i] & 0xff)<<(i*8);
         }
     }
     else
     {
         for (int i=0;i<blen;i++) {
             num |= (bytes[i] & 0xff)<<((blen-i-1)*8);
         }
     }
     return true;
}

void Numeric2HexStr(const unsigned long &num,char *strBuf,int byts_Len,bool small_head)
{
     unsigned char l4b,r4b;
     char l4c,r4c;
     int i;
     unsigned char byts[byts_Len];
     if(small_head)//小端
     {
         for (int i=0;i<byts_Len;i++) {
             byts[i]=(num >> (i*8)) & 0xff;
         }
     }
     else
     {
         for (int i=0;i<byts_Len;i++) {
             byts[i]=(num >> ((byts_Len-i-1)*8)) & 0xff;
         }
     }

     for(i=0;i<byts_Len;i++)
     {
          r4b=byts[i] & 0x0f;
          if(r4b<=9)
              r4c=0x30+r4b;
          else
              r4c=0x41+(r4b-0x0a);

          l4b=(byts[i] & 0xf0) >> 4;
          if(l4b<=9)
              l4c=0x30+l4b;
          else
              l4c=0x41+(l4b-0x0a);

          strBuf[i*2]=l4c;
          strBuf[i*2+1]=r4c;
     }
}
bool HexStr2Numeric(const char *strBuf,bool small_head,unsigned long &num)
{
    int blen=8;
    unsigned char tmp[8]={0};
     if(!HexStrToBytes(strBuf,tmp,blen))
         return false;
    \
     if(small_head)//小端
     {
         for (int i=0;i<blen;i++) {
             num |= (tmp[i] & 0xff)<<(i*8);
         }
     }
     else
     {
         for (int i=0;i<blen;i++) {
             num |= (tmp[i] & 0xff)<<((blen-i-1)*8);
         }
     }
     return true;
}

//判断是否 IP4 地址
bool isIP4AddrString(const char *strBuf,int len)
{
    struct in_addr s;
    char tmp[16]={'\0'};
    if(len>sizeof(tmp))
        len=sizeof(tmp);
    memcpy(tmp,strBuf,len);
    int rtn=inet_pton(AF_INET,tmp,(void*)&s);
    if(rtn==1)
    {
        printf("the ip address value is %d\n",s.s_addr);
        return true;
    }
    else
    {
        printf("the ip address value is %d\n",s.s_addr);
        return false;
    }
}
